package com.easyexcle.demo.easyExcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.easyexcle.demo.easyExcel.style.HeadHorizontalCellStyleStrategy;
import com.easyexcle.demo.easyExcel.style.TitleColorSheetWriteHandler;
import com.easyexcle.demo.easypoi.domain.ExportVo;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author: lkz
 * @Title: Test2
 * @Description: 测试同一个sheet里面 多个数据是数据Table
 * @Date: 2023/6/9 15:02
 */

public class Test2 {


    public static void main(String[] args) {
        try {
            // 方法1 如果写到同一个sheet
            String fileName =  "D:\\" + System.currentTimeMillis() + ".xlsx";
            // 这里直接写多个table的案例了，如果只有一个 也可以直一行代码搞定，参照其他案例
            ExcelWriter excelWriter = null;
            try {

                List<ExportVo> exportVos = Test2.dataListEntity();
                List<List<String>> head = Test2.head();
                List<List<Object>> lists = Test2.dataList();

                // 这里不指定class
                excelWriter = EasyExcel.write(fileName).registerWriteHandler(new HorizontalCellStyleStrategy(HeadHorizontalCellStyleStrategy.getHeadStyle(),
                        HeadHorizontalCellStyleStrategy.getContentStyle())).build();
                // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
                WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.TRUE).build();
                // 这里必须指定需要头，table 会继承sheet的配置，sheet配置了不需要，table 默认也是不需要
                WriteTable writeTable0 = EasyExcel.writerTable(0).head(head).build();

                // 第二个对象 读取对象的excel实体类中的标题
                WriteTable writeTable1 = EasyExcel.writerTable(1).head(ExportVo.class).needHead(Boolean.TRUE).build();

                WriteTable writeTable2 = EasyExcel.writerTable(2).head(head).build();
                // 第一次写入会创建头
                excelWriter.write(lists, writeSheet, writeTable0);

                // 第二次写如也会创建头，然后在第一次的后面写入数据
                excelWriter.write(exportVos, writeSheet, writeTable1);
                excelWriter.write(lists, writeSheet, writeTable2);
            } finally {
                // 千万别忘记finish 会帮忙关闭流
                if (excelWriter != null) {
                    excelWriter.finish();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static List<ExportVo> dataListEntity(){
        List<ExportVo> list=new ArrayList<>();
        for(int i=0;i<100;i++) {
            ExportVo exportVo = new ExportVo();
            exportVo.setCode(String.valueOf(i));
            exportVo.setCodeName("张" + i);

            exportVo.setStatus(String.valueOf(i));
            exportVo.setSuijiCode(String.valueOf(i));
            exportVo.setSuijiMan(String.valueOf(i));
            exportVo.setTime(new Date());
            exportVo.setCoreCode(String.valueOf(i));
            list.add(exportVo);
        }
        list.add(new ExportVo());
       return list;

    }

    private static List<List<String>> head() {
        List<List<String>> list = new ArrayList<>();
        List<String> head0 = new ArrayList<>();
        String empty=" ";
        head0.add("访视表");
        head0.add("eCRF/Visit");
        List<String> head1 = new ArrayList<>();
        head1.add(empty);
        head1.add("筛选期");
        List<String> head2 = new ArrayList<>();
        head2.add(empty);
        head2.add("术后辅助_CnD1");
        List<String> head3 = new ArrayList<>();
        head3.add(empty);
        head3.add("疾病进展后治疗_CnD1");
        List<String> head4 = new ArrayList<>();
        head4.add(empty);
        head4.add("肿瘤评估");
        List<String> head5 = new ArrayList<>();
        head5.add(empty);
        head5.add("日志");
        List<String> head6 = new ArrayList<>();
        head6.add(empty);
        head6.add("计划外访视");

        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);
        list.add(head6);
        return list;
    }

    private static List<List<Object>> dataList() {
        List<List<Object>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<Object> data = new ArrayList<>();
            data.add("字符串" + i);
            data.add("0.56-------------\n 是 测试数据√ 研究数据√ \n 否 测试名字 √");
            data.add(new Date());
            list.add(data);
        }
        // 添加一行 空数组 空格
        list.add(Collections.emptyList());
        return list;
    }

    //配置字体，表头背景等
    private static HorizontalCellStyleStrategy setConfigure() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        WriteFont headWriteFont = new WriteFont();
        // 加粗
        headWriteFont.setBold(false);
//        headWriteFont.setFontHeightInPoints((short) 14);
        headWriteCellStyle.setWriteFont(headWriteFont);


        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 字体策略
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
//        contentWriteFont.setFontHeightInPoints((short) 14);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        //边框
        //导出数据垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //导出数据水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setBorderLeft(BorderStyle.NONE);
        contentWriteCellStyle.setBorderTop(BorderStyle.NONE);
        contentWriteCellStyle.setBorderRight(BorderStyle.NONE);
        contentWriteCellStyle.setBorderBottom(BorderStyle.NONE);

        //设置 自动换行
        contentWriteCellStyle.setWrapped(true);
        //设置
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }






}
